﻿// ActionScript file
/**
 * 数据驱动的清�?
 * 
 * author	:com.nickro
 * 
 * USAGE	:
 * 			var list:List=new List(dataProvider:(Array/XMLList/XML),listItem:Class);
 * 			addChild(list);
 * 			list.edge=new EdgeMetrics();
 * 			list.direction="(vertical/horizontal";
 * 			list.horizontalGap=(Number);
 * 			list.verticalGap=(Number);
 * 			list.startIndex=(Number);
 * 
 * Problems	:
 * 			因为布局操作的延迟进行导致无法获得真实的尺寸�?
 * 			而不进行布局属性延迟的操作就必须在创建组件之前给这些属性赋值，即在构造函数时即给属性赋值；
 * 				这将给组件的扩展性带来很大不�?
 * 			P:此已非动态添加内容的问题，该问题已解�?
 * 			通过创建动态内容之前就用背景填充动态内容容�?参见Image);
 * 
 * 			妥协方法:Line78,派发事件，外部也必须监听才能获取真实的尺�?
 * expands	:
 * 			1.listItem类型要求即可以是Class也可以是DisplayObject;
 */
package org.nickro.controls{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import org.nickro.events.BaseEvent;
	import org.nickro.events.ItemClickEvent;
	
	public class List extends BaseControls{
		public static const CREATION_COMPLETE:String="creation_complete";
		private var _direction:String;
		private var _horizontalGap:Number=0;
		private var _verticalGap:Number=0; 
		private var _startIndex:int=-1;
		
		private var _item:Object;
		private var _selectedIndex:int;
		private var items:Array;
		private var itemNum:int;
		public function List(dataProvider:Object=null,listItem:*=null){
			_item=listItem;
			super(dataProvider);
		}
		override protected function updateDislayObject():void{
			super.updateDislayObject();
			var childs:Array=new Array;
			var totalX:Number=0;
			var totalY:Number=0;
			
			for(var i:int=0;i<itemNumber;i++){
				var child:DisplayObject=this.getChildAt(i);
				childs.push(child);
			}
			switch(_direction){
				case "horizontal":
					for(var i:int=0;i<itemNumber;i++){
						childs[i].x = totalX;
						totalX += _horizontalGap+childs[i].width;
					}
				break;
				case "vertical":
					for(var i:int=0;i<itemNumber;i++){
						childs[i].y = totalY;
						totalY += _verticalGap + childs[i].height/childs[i].scaleY;
					}
				break;
			}
			/**
			 * 更新容器的宽�? 设置背景
			 */
			dispatchEvent(new BaseEvent(BaseEvent.CREATION_COMPLETE));
			onSelected(_startIndex);
			
		}
	 	override protected function createChildren():void{
	 		items=new Array();
			for(var i:int=0;i<itemNumber;i++){
				/* if(_item is Class){
					items[i]=new _item();
				}
				if(_item is MovieClip){
					items[i]=_item;
				}  */
				items[i]=new _item();
				items[i].dataProvider=data[i];
				addChild(items[i]);
				items[i].addEventListener(MouseEvent.MOUSE_DOWN,onItemDown);
			}
		}
	
		/**
		 * Event Handlers
		 */
		private function onItemDown(e:MouseEvent):void{
			_selectedIndex=getChildIndex(DisplayObject(e.target));
			onSelected(_selectedIndex);
		} 
		private function onSelected(i:int):void{
			if(i == -1){
				return;
			}else{
				items[i].selected=true;
				dispatchEvent(new ItemClickEvent(ItemClickEvent.ITEM_CLICK,i));
				for(var j:int=0;j<itemNumber;j++){
					if(j != i){
						items[j].selected=false;
					}
				}
			}
		}
		 
		
		/**
		 * public setter/getter
		 */
		public function set startIndex(value:int):void{
			_startIndex=value;
			setChanged();
		}
		public function set selectedIndex(value:int):void{
			_selectedIndex=value;
			onSelected(_selectedIndex);
		}

		public function get direction():String{
			return _direction;
		}
		public function set direction(value:String):void{
			_direction=value;
			setChanged();
		}
		
		public function get horizontalGap():Number{
			return _horizontalGap;
		}
		public function set horizontalGap(value:Number):void{
			_horizontalGap=value;
			setChanged();
		}
		
		public function get verticalGap():Number{
			return _verticalGap;
		}
		public function set verticalGap(value:Number):void{
			_verticalGap=value;
			setChanged();
		}
	}
}